package com.amado.course34Thread;

/**
 * 
 * 
 * 实现线程有两种方式： 1、继承Thread类 重写run方法， 2、实现Runnable接口，实现run方法，
 * 
 * 通过start启动一个线程
 * 
 */
public class S08RunableJoinTest {
	public static void main(String[] args) {
		/**
		 * 启动一个线程使用Thread.start()
		 */
		StringBuffer sbOne = new StringBuffer();
		Thread threadOne = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				sbOne.append("a");
			}
		});
		
		StringBuffer sbTwo = new StringBuffer();
		Thread threadTwo = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				sbTwo.append("b");
			}
		});
		threadOne.start();
		threadTwo.start();
		try {
			/**
			 * 两个线程做两件事 两件事要汇总起来，组装
			 */
			//等待该线程终止(threadOne)
			threadOne.join();
			//等待该线程终止(threadTwo)
			threadTwo.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("组装");
		System.out.println("数据"+sbOne.toString()+sbTwo.toString());
	}
}
